package zunge.tools;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.security.NoSuchProviderException;

public class SHA512Hash {
	
	static MessageDigest md = null;
	
	public static String hash(String input) throws NoSuchAlgorithmException, NoSuchProviderException {
		if(md==null){md = MessageDigest.getInstance("SHA-512");}
		
		//SHA-512 hash	
		md.update(input.getBytes());
		byte[] bytes = md.digest();
		
		//Convert to Hex string
		StringBuffer sb = new StringBuffer();
		for(int i = 0;i < bytes.length;++i){
			//String h = Integer.toString(bytes[i] & 0xFF); // This method will omit the beginning "0" for the two chars.
			String h = Integer.toString((bytes[i] & 0xFF) + 0x100, 16).substring(1); //Fixed length of 128 chars.
			sb.append(h);
		}
		
		return sb.toString();
	}

}
